package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_mass_mailing_test;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_mass_mailing_testSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_mass_mailing_testService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[示例邮件向导] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_mass_mailing_testServiceImpl implements IMail_mass_mailing_testService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_mass_mailing_testClientService mail_mass_mailing_testClientService;

    @Override
    public boolean update(Mail_mass_mailing_test mail_mass_mailing_test) {
        boolean bOk = false ;
        Imail_mass_mailing_test clientModel = this.convert2Model(mail_mass_mailing_test,null);
        mail_mass_mailing_testClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_mass_mailing_test) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Mail_mass_mailing_test mail_mass_mailing_test) {
        boolean bOk = false ;
        Imail_mass_mailing_test clientModel = this.convert2Model(mail_mass_mailing_test,null);
        mail_mass_mailing_testClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_mass_mailing_test);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Mail_mass_mailing_test> mail_mass_mailing_tests, int batchSize) {

        //mail_mass_mailing_testClientService.updateBatch(mail_mass_mailing_tests,batchSize) ;

    }

    @Override
    public void createBatch(List<Mail_mass_mailing_test> mail_mass_mailing_tests, int batchSize) {

                        //mail_mass_mailing_testClientService.createBatch(mail_mass_mailing_tests,batchSize) ;

    }

    @Override
    public Mail_mass_mailing_test get(Integer id) {
        Imail_mass_mailing_test clientModel = mail_mass_mailing_testClientService.createModel();
        clientModel.setId(id);
        mail_mass_mailing_testClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_mass_mailing_test get(Mail_mass_mailing_test mail_mass_mailing_test) {
        Mail_mass_mailing_test retmail_mass_mailing_test = this.get(mail_mass_mailing_test.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_mass_mailing_test.getClass(), mail_mass_mailing_test.getClass(), false);
        copier.copy(retmail_mass_mailing_test, mail_mass_mailing_test, null);
        return mail_mass_mailing_test ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_mass_mailing_test clientModel = mail_mass_mailing_testClientService.createModel();
        clientModel.setId(id);
        mail_mass_mailing_testClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_mass_mailing_testClientService.removeBatch(mail_mass_mailing_tests) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_mass_mailing_test> searchDefault(Mail_mass_mailing_testSearchContext context) {
        List<Mail_mass_mailing_test> list = new ArrayList<Mail_mass_mailing_test>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test> clientModelList = mail_mass_mailing_testClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_test>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据群发邮件查询
     */
    @Override
    public Page<Mail_mass_mailing_test> selectByOdoo_mass_mailing(Integer id){
        Mail_mass_mailing_testSearchContext context = new Mail_mass_mailing_testSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_test> list = new ArrayList<Mail_mass_mailing_test>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test> clientModelList = mail_mass_mailing_testClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_test>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mass_mailing_test> selectByOdoo_create(Integer id){
        Mail_mass_mailing_testSearchContext context = new Mail_mass_mailing_testSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_test> list = new ArrayList<Mail_mass_mailing_test>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test> clientModelList = mail_mass_mailing_testClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_test>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mass_mailing_test> selectByOdoo_write(Integer id){
        Mail_mass_mailing_testSearchContext context = new Mail_mass_mailing_testSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_test> list = new ArrayList<Mail_mass_mailing_test>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test> clientModelList = mail_mass_mailing_testClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_test clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_test>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_mass_mailing_test convert2Model(Mail_mass_mailing_test domain , Imail_mass_mailing_test model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_mass_mailing_testClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getEmail_toDirtyFlag())
            model.setEmail_to(domain.getEmail_to());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getMass_mailing_id_textDirtyFlag())
            model.setMass_mailing_id_text(domain.getMass_mailing_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getMass_mailing_idDirtyFlag())
            model.setMass_mailing_id(domain.getMass_mailing_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Mail_mass_mailing_test convert2Domain( Imail_mass_mailing_test model ,Mail_mass_mailing_test domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_mass_mailing_test();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getEmail_toDirtyFlag())
            domain.setEmail_to(model.getEmail_to());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getMass_mailing_id_textDirtyFlag())
            domain.setMass_mailing_id_text(model.getMass_mailing_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getMass_mailing_idDirtyFlag())
            domain.setMass_mailing_id(model.getMass_mailing_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
